package com.gopay.checkorder.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.checkorder.service.BankOrderService;
import com.gopay.checkorder.service.task.BankCountDownLatch;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.constants.bank.CmbcConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BankOrder;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cmbc.ebank.CmbcMessage;
import com.gopay.common.domain.bank.dayend.BaseDayendEntry;
import com.gopay.common.domain.cashier.BankCardType;
import com.gopay.common.domain.cps.vo.CheckOrderResult;
import com.gopay.common.domain.cps.vo.InAccInfo;
import com.gopay.remote.bank.BankInteractRemoteService;

public class CheckOrderCmbcB2cImpl implements BankOrderService {
	private final static Logger logger = LoggerFactory.getLogger("CMBC");

	@Resource(name = "bankClient")
	private BankInteractRemoteService bankClient;
	@Override
	public CheckOrderResult checkOrder(BankOrder bankOrder, String target) {
		CheckOrderResult checkOrderResult = new CheckOrderResult();
		checkOrderResult.setSuccess(false);
		checkOrderResult.setOrderId(bankOrder.getOrderId());
		try {

			CmbcMessage base = new CmbcMessage();
			base.setOrderId(bankOrder.getOrderId());
			base.setBankOrderId(bankOrder.getOutOrderId());
			base.setBank(BankConstants.BANK_LOGO_CMBC);
			base.setBankIntrChann(BisBankInterfaceChannel.get(bankOrder.getInterfaceChennelCode()));
			base.setBankBusProd(BisBankBusinessProd.get(bankOrder.getBankBizProdCode()));

			Result result = new Result();
			result = bankClient.query(base);

			if (result == null || !result.isSuccess()) {
				logger.error("bankClient fail.");
				if(result ==null || ProcCodeConstants.PROC_CODE_100E6037.equals(result.getResultCode())){
					BankCountDownLatch.failIncrease(bankOrder.getBankCode());
					checkOrderResult.setCommunicateError(true);
				}
				logger.error(ToStringBuilder.reflectionToString(result,ToStringStyle.DEFAULT_STYLE));
				checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E6022);
				checkOrderResult.setErrMsg("系统返回对账结果为空");
				return checkOrderResult;
			}
			CmbcMessage msg = null;
			if (result != null && result.isSuccess()) {
				msg = (CmbcMessage) result.getDefaultModel();
			}
			// 返回结果为null
			if (msg == null) {
				logger.error("民生银行B2C 对账失败，订单查询返回结果为null，orderId："+ bankOrder.getOrderId());

				checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E5062);
				checkOrderResult.setErrMsg("银行返回对账结果为空");
				return checkOrderResult;
			}
			if ((msg.getOrderAmt() == null || msg.getOrderNo() == null || msg.getOrderStatus() == null) && msg.getRemark() != null) {
				logger.info("民生B2C订单" + bankOrder.getOrderId()+ msg.getRemark());
				checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E5062);
				checkOrderResult.setErrMsg("bis query is null.");
				return checkOrderResult;
			}
			if (msg.getOrderStatus().equals(CmbcConstants.QUERY_ORDER_FAIL)) {
				checkOrderResult.setBankFailure(true);
				logger.info("民生B2C订单" + bankOrder.getOrderId() + "的交易失败");
				checkOrderResult.setErrMsg(msg.getResMessage());

			} else if (msg.getOrderStatus().equals(CmbcConstants.QUERY_ORDER_SUCC)) {
				logger.info("民生B2C订单" + bankOrder.getOrderId() + "的交易成功");
				checkOrderResult.setSuccess(true);
				InAccInfo accInfo = new InAccInfo();
				accInfo.setOrderId(bankOrder.getOrderId());
				accInfo.setAmount(new BigDecimal(msg.getOrderAmt()));
				accInfo.setRetCode("00");
				accInfo.setCardType(BankCardType.Unknown.toString());
				checkOrderResult.setAccInfo(accInfo);
			} else if (msg.getOrderStatus().equals(CmbcConstants.QUERY_ORDER_EXCEP)) {
				logger.info("民生B2C订单" + bankOrder.getOrderId()+ "订单状态异常！请联系银行。");
	            checkOrderResult.setErrMsg("订单状态异常！请联系银行");
			} else if (msg.getOrderStatus().equals(CmbcConstants.QUERY_ORDER_BANKFAIL)) {
				logger.info("民生B2C订单" + bankOrder.getOrderId() + "银行后台查询失败");				      
	            checkOrderResult.setErrMsg("银行后台查询失败");
			} else if (msg.getOrderStatus().equals(CmbcConstants.QUERY_ORDER_DBERROR)) {
				logger.info("民生B2C订单" + bankOrder.getOrderId() + "银行数据库错误");
	            checkOrderResult.setErrMsg("银行数据库错误");				

			} else if (msg.getOrderStatus().equals(CmbcConstants.QUERY_ORDER_NOFIND)) {
				logger.info("民生B2C订单" + bankOrder.getOrderId() + "未找到");
	            checkOrderResult.setErrMsg("银行订单不存在");
			} else {
				logger.info("民生B2C	订单" + bankOrder.getOrderId() + "交易发生异常！跳出");
			}
			return checkOrderResult;
		} catch (Exception e) {
			checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E6022);
			checkOrderResult.setErrMsg("对账调用bis失败");
			return checkOrderResult;
		}
	}
	@Override
	public BankOrder getDbOrder() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public BaseMessage dayend(String bank_bus) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public List<CheckOrderResult> checkDayend(String bank_bus) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Result getDayendMsg(String string) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public CheckOrderResult checkDayendOrder(BaseDayendEntry bdm, BankOrder bo) {
		// TODO Auto-generated method stub
		return null;
	}
}
